package com.conference.controller;

import com.conference.entity.Conference;
import com.conference.service.ConferenceService;
import com.conference.entity.UserConference;
import com.conference.service.UserConferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/conferences")
public class ConferenceController {

    @Autowired
    private ConferenceService conferenceService;

    @Autowired
    private UserConferenceService userConferenceService;

    @PostMapping
    public ResponseEntity<Conference> createConference(@RequestBody Conference conference) {
        conference.setOrganizerId(0L);
        return ResponseEntity.ok(conferenceService.createConference(conference));
    }

    @PutMapping("/{id}")
    public ResponseEntity<Conference> updateConference(@PathVariable Long id, @RequestBody Conference conference) {
        conference.setId(id);
        return ResponseEntity.ok(conferenceService.updateConference(conference));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteConference(@PathVariable Long id) {
        conferenceService.deleteConference(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Conference> getConference(@PathVariable Long id) {
        return ResponseEntity.ok(conferenceService.getConferenceById(id));
    }

    @GetMapping
    public ResponseEntity<Page<Conference>> getAllConferences(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String location,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate
    ) {
        Pageable pageable = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "createTime"));
        return ResponseEntity.ok(conferenceService.searchConferences(
            title, location, status, startDate, endDate, pageable
        ));
    }

    @GetMapping("/{id}/notice")
    public ResponseEntity<String> getConferenceNotice(@PathVariable Long id) {
        Conference conference = conferenceService.getConferenceById(id);
        return ResponseEntity.ok(conference.getNoticeContent());
    }

    @GetMapping("/{id}/qrcode")
    public ResponseEntity<String> getConferenceQRCode(@PathVariable Long id) {
        Conference conference = conferenceService.getConferenceById(id);
        return ResponseEntity.ok(conference.getQrCodeUrl());
    }

    /**
     * 获取会议的参会人员座位信息
     */
    @GetMapping("/{conferenceId}/import-seats")
    public ResponseEntity<Map<String, Object>> getConferenceSeats(@PathVariable Long conferenceId) {
        List<UserConference> seats = userConferenceService.getConferenceSeats(conferenceId);
        Map<String, Object> result = Map.of(
            "success", true,
            "message", "获取座位信息成功",
            "data", seats
        );
        return ResponseEntity.ok(result);
    }
} 